package com.example.demo.service.imply;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.annotation.AutoFill;
import com.example.demo.enumeration.OperationType;
import com.example.demo.mapper.ComponentMapper;
import com.example.demo.mapper.InventoryMapper;
import com.example.demo.mapper.StorageMapper;
import com.example.demo.pojo.PublicFields;
import com.example.demo.pojo.dto.ComponentDTO;
import com.example.demo.pojo.entity.Component;
import com.example.demo.pojo.entity.Storage;
import com.example.demo.pojo.query.ComponentQuery;
import com.example.demo.pojo.vo.ComponentVO;
import com.example.demo.pojo.vo.PageVO;
import com.example.demo.service.IComponentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class IComponentsServiceImply extends ServiceImpl<ComponentMapper, Component> implements IComponentService {
    private final StorageMapper StorageMapper;
    private final InventoryMapper inventorymapper;
    @Override
    @AutoFill(OperationType.INSERT)
    public void addComponent(ComponentDTO componentDTO, PublicFields publicFields) {
        Component component = new Component();
        BeanUtils.copyProperties(componentDTO, component);
        BeanUtils.copyProperties(publicFields, component);
        baseMapper.insert(component);
    }

    @Override
    @AutoFill(OperationType.UPDATE)
    public void updateComponent(ComponentDTO componentDTO, PublicFields publicFields) {
        Component component = new Component();
        BeanUtils.copyProperties(componentDTO, component);
        BeanUtils.copyProperties(publicFields, component);

        LambdaUpdateWrapper<Component> wrapper = new LambdaUpdateWrapper<Component>()
                .set(StringUtils.hasText(component.getComponentName()), Component::getComponentName, component.getComponentName())
                .set(component.getPrice() != null, Component::getPrice, component.getPrice())
                .set(component.getUpdateTime() != null, Component::getUpdateTime, component.getUpdateTime())
                .set(StringUtils.hasText(component.getDescription()), Component::getDescription, component.getDescription())
                .eq(Component::getComponentId, component.getComponentId());

        baseMapper.update(wrapper);
    }

    @Override
    public void deleteComponent(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public PageVO<ComponentVO> queryComponentPage(ComponentQuery componentQuery) {
        Page<Component> p = Page.of(componentQuery.getPageNo(), componentQuery.getPageSize());
        p.addOrder(OrderItem.desc("update_time"));
        Page<Component> page = lambdaQuery()
                .like(StringUtils.hasText(componentQuery.getComponentName()), Component::getComponentName, componentQuery.getComponentName())
                .like(StringUtils.hasText(componentQuery.getCategory()), Component::getCategory, componentQuery.getCategory())
                .page(p);

        PageVO<ComponentVO> componentVOs = new PageVO<>();

        componentVOs.setPages(p.getPages());
        componentVOs.setTotal(p.getTotal());

        List<Component> records = p.getRecords();
        if(CollectionUtils.isEmpty(records)){
            componentVOs.setList(Collections.emptyList());
            return componentVOs;
        }

        List<ComponentVO> componentVOList = records.stream().map(component -> {
            ComponentVO componentVO = new ComponentVO();
            BeanUtils.copyProperties(component, componentVO);

            return componentVO;
        }).collect(Collectors.toList());

        componentVOs.setList(componentVOList);

        return componentVOs;
    }

    @Override
    public ComponentVO getComponentById(Long id) {
        LambdaQueryWrapper<Component> wrapper = new LambdaQueryWrapper<Component>()
                .eq(Component::getComponentId, id);

        Component component = baseMapper.selectOne(wrapper);
        ComponentVO componentVO = new ComponentVO();
        BeanUtils.copyProperties(component, componentVO);
        

        return componentVO;
    }
}
